package com.canlihao.ui.view.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.canlihao.ui.R;
import com.canlihao.ui.base.BaseWhiteTopbarActivity;
import com.canlihao.ui.utils.MyLog;
import com.canlihao.ui.view.fragment.UpdateDialogFragment;
import com.zyq.easypermission.EasyPermission;
import com.zyq.easypermission.EasyPermissionHelper;
import com.zyq.easypermission.EasyPermissionResult;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*******
 *
 *Created by panda李
 *
 *创建时间 2020/2/10
 *
 *类描述：
 *
 ********/
public class PermissionActivity extends BaseWhiteTopbarActivity {
    private static final String TAG = "PermissionActivity";
    @BindView(R.id.btn_permission)
    Button btnPermission;
    EasyPermission easyPermission;
    String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO};
    @Override
    public int getLayoutResId() {
        return R.layout.permission_activity;
    }

    @Override
    protected void initView() {
        super.initView();
        mTopBar.setTitle("loading");
    }

    @Override
    public void initData() {
        super.initData();
//        updateDialog = new UpdateDialog(LoadingActivity.this,"1、这是一个测试的点击弹出更新提示；",
//                "http://cnalihao.oss-cn-chengdu.aliyuncs.com/app/sansongtv_lastest.apk","0");
        //https://static.ss-clouddoctor.com/app/sansongtv_lastest.apk
        btnPermission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                permissions();
            }
        });
    }
    public void permissions(){
        if (Build.VERSION.SDK_INT >= 23) {
            if(EasyPermission.build().hasPermission(PermissionActivity.this,perms )){
                MyLog.i(TAG,"guole```.0");
            }else{
                easyPermission = EasyPermission.build()
                        .mRequestCode(10)
                        .mContext(PermissionActivity.this)
                        .mPerms(perms)
                        .mResult(new EasyPermissionResult() {
                            @Override
                            public void onPermissionsAccess(int requestCode) {
                                super.onPermissionsAccess(requestCode);
                                //做你想做的
                                    MyLog.i(TAG,requestCode+"做你想做的.0");
                            }

                            @Override
                            public void onPermissionsDismiss(int requestCode, @NonNull List<String> permissions) {
                                super.onPermissionsDismiss(requestCode, permissions);
                                //你的权限被用户拒绝了你怎么办
                                MyLog.i(TAG,requestCode+"你的权限被用户拒绝了你怎么办.0");
                                permissions();
                            }

                            @Override
                            public boolean onDismissAsk(int requestCode, @NonNull List<String> permissions) {
                                //你的权限被用户禁止了并且不能请求了你怎么办
                                MyLog.i(TAG,"你的权限被用户禁止了并且不能请求了你怎么办.0");
                                permissions();
                                //easyPermission.openAppDetails(PermissionActivity.this, "Call Phone - Give me the permission to dial the number for you");
                                return true;
                            }
                        });
                easyPermission.requestPermission();
            }

        }else{
            MyLog.i(TAG,"版本小于6.0");
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (EasyPermission.APP_SETTINGS_RC == requestCode) {
            //设置界面返回
            //Result from system setting
            if (easyPermission.hasPermission(PermissionActivity.this)) {
                //做你想做的
            } else {
                //从设置回来还是没给你权限
            }

        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //使用EasyPermissionHelper注入回调
        EasyPermissionHelper.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
